package org.momo.rpc.client;

import io.netty.channel.Channel;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelHolder {
    private final static Map<InetSocketAddress, Channel> channelMap = new ConcurrentHashMap<>();

    public static Channel getChannel(InetSocketAddress inetSocketAddress) {
        return channelMap.get(inetSocketAddress);
    }

    public static void putChannel(InetSocketAddress inetSocketAddress, Channel channel) {
        channelMap.put(inetSocketAddress, channel);
    }

    public static void closeChannel(InetSocketAddress inetSocketAddress) {
        Channel channel = channelMap.get(inetSocketAddress);
        if (channel != null) {
            channel.close();
        }
        channelMap.remove(inetSocketAddress);
    }

    public static void closeAllChannel() {
        for (Channel channel : channelMap.values()) {
            channel.close();
        }
    }
}
